/**
 * weapp-js v1.7.1(2021-4-16)
 * https://codeup.aliyun.com/motigo/motigo/weapp_base
 * Copyright@ 2021 Motigo, Inc.
 * 
 */
export default {
    showModal(opts) {
        const pages = getCurrentPages();
        const page = pages.length > 0 ? pages[pages.length - 1] : null;
        const ui = page ? page.selectComponent('#__weapp_ui__') : null;

        if (ui) {
            ui.showModal(opts);
        } else {
            wx.showModal(opts);
            console.error(`no weapp-ui node in page! pleass add this line  to page wxml: \n\t<weapp-ui id='__weapp_ui__'></weapp-ui>`);
        }
    },
    previewImage(opts) {
        const pages = getCurrentPages();
        const page = pages.length > 0 ? pages[pages.length - 1] : null;
        const ui = page ? page.selectComponent('#__weapp_ui__') : null;

        if (ui) {
            ui.previewImage(opts);
        } else {
            wx.previewImage(opts);
            console.error(`no weapp-ui node in page! pleass add this line  to page wxml: \n\t<weapp-ui id='__weapp_ui__'></weapp-ui>`);
        }
    },
    showLoading(opts) {
        this.enable_show_loading = true;
        setTimeout(() => {
            if (this.enable_show_loading) {
                wx.showLoading(opts);
            }
        }, 100);
    },
    hideLoading() {
        this.enable_show_loading = false;
        wx.hideLoading();
    },
    login(opts) {
        const node = this._get_ui_root();

        if (node) {
            return node.login(opts);
        } else {
            const errMsg = `no weapp-ui node in page! pleass add this line  to page wxml: \n\t<weapp-ui id='__weapp_ui__'></weapp-ui>`;
            console.error(errMsg);
            return new Promise((_, reject) => {
                reject({
                    errCode: 'MISSING_WEAPP_UI_NODE',
                    errMsg: errMsg
                });
            });
        }
    },
    _get_ui_root() {
        const pages = getCurrentPages();
        const page = pages.length > 0 ? pages[pages.length - 1] : null;
        return page ? page.selectComponent('#__weapp_ui__') : null;
    }
}